﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for clsTaoDeThi
/// </summary>
public class clsTaoDeThi:BLL
{
    DataClassesDataContext obj_taodt = new DataClassesDataContext();
    public clsTaoDeThi()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    private string MaDT;

    public string MaDT1
    {
        get { return MaDT; }
        set { MaDT = value; }
    }


    public List<clsTaoDeThi> danhsachdt()
    {
        var dethi = (from p in obj_taodt.tbl_DETHIs
                      select new clsTaoDeThi{ 
                        MaDT=p.MaDT
                     }).ToList();
        return dethi;
    }
    public string retuMadtmax()
    {
        int idmax = obj_taodt.tbl_DETHIs.Max(q => q.Iddethi);
        string madtmax = (from p in obj_taodt.tbl_DETHIs
                       where p.Iddethi == idmax
                       select p.MaDT).Single().ToString();
        return madtmax;
 
    }
    /*Bat dau ham kiem tra so cau hoi theo muc do cua ma chuong*/
    public bool ktraCHofChuong(int mucdo,List<string> listmachuong, int soCHofMucDo)
    {
        int sumCHtheoMucDo = (from p in obj_taodt.tbl_NGANHANGCAUHOIs
                    where p.Mucdo == mucdo && listmachuong.Contains(p.MaChuong)
                    select p).Count();
        if (sumCHtheoMucDo<soCHofMucDo)
        {
            return false;
        }
        return true;
 
    }

    /*Ket thuc ham kiem tra cau hoi ma chuong*/

    /*Bat dau kiem tra theo muc do */

    public int KQKT(string maqt, List<string> listmachuong)
    {
        int kho = (from p in obj_taodt.tbl_QUYTACDETHIs
                   where p.MaQT == maqt
                   select p.Kho).Single();
        int tb = (from p in obj_taodt.tbl_QUYTACDETHIs
                  where p.MaQT == maqt
                  select p.TB).Single();
        int de = (from p in obj_taodt.tbl_QUYTACDETHIs
                  where p.MaQT == maqt
                  select p.De).Single();

        if (!ktraCHofChuong(3, listmachuong, kho))
        {
            return 1;
        }
        if (!ktraCHofChuong(2, listmachuong, tb))
        {
            return 2;
        }
        if (!ktraCHofChuong(1, listmachuong, de))
        {
            return 3;
        }
        return 0;
 
    }
    /*Ket thuc kiem tra theo muc do */

    public void TaoDeThi(string maqt,List<string> listmachuong)
    {
        string madt = retuMadtmax();
        int kho = (from p in obj_taodt.tbl_QUYTACDETHIs
                   where p.MaQT == maqt
                   select p.Kho).Single();
        //int tb = (from p in obj_taodt.tbl_QUYTACDETHIs
        //          where p.MaQT == maqt
        //          select p.TB).Single();
        //int de = (from p in obj_taodt.tbl_QUYTACDETHIs
        //          where p.MaQT == maqt
        //          select p.De).Single();
        string sokho = kho.ToString();
        string arrMachuong = string.Empty;
        foreach (var item in listmachuong)
        {
            if (arrMachuong.Length>0)
            {
                arrMachuong += ",";

            }
            arrMachuong += "'"+ item+"'" ;
        }

        obj_taodt.sp_LayRDCH(sokho, madt, "3", arrMachuong);

        int tb = (from p in obj_taodt.tbl_QUYTACDETHIs
                  where p.MaQT == maqt
                  select p.TB).Single();
        string sotb = tb.ToString();
        obj_taodt.sp_LayRDCH(sotb, madt, "2", arrMachuong);

        int de = (from p in obj_taodt.tbl_QUYTACDETHIs
                  where p.MaQT == maqt
                  select p.De).Single();
        string sode = de.ToString();
        obj_taodt.sp_LayRDCH(sode, madt, "1", arrMachuong);

        obj_taodt.SubmitChanges();
        
    }
    public void ThemDeThi(string maqt, string tendt,string maloptc, DateTime ngayapdung)
    {
        tbl_DETHI dt = new tbl_DETHI();
        int idmaxs = (int)obj_taodt.sp_GetCurrentIndNew("tbl_DETHI").ReturnValue;

        string madt = TaoMaAuto("MaDT", "0000", idmaxs);

        dt.MaDT = madt;
        dt.NgayApDung = ngayapdung;
        dt.MaQT = maqt;
        //dt.MsMonHoc = mamon;
        dt.TenDeThi = tendt;
        dt.MsLopTC = maloptc;
        obj_taodt.tbl_DETHIs.InsertOnSubmit(dt);
        obj_taodt.SubmitChanges();
 
    }
    public void XoaDT(string madt,string date,string nguoixoa)
    {
        var xoadt = (from p in obj_taodt.tbl_DETHIs
                     where p.MaDT == madt
                     select p).Single();
        DateTime ngayxoa = Convert.ToDateTime(date);
        xoadt.Ngayxoa = ngayxoa;
        xoadt.Nguoixoa = nguoixoa;
        xoadt.Deleted = true;
        obj_taodt.SubmitChanges();

    }
    public void hoandoiCH(string machleft,string machright,string madt)
    {
        tbl_CHITIETDETHI ctdt = new tbl_CHITIETDETHI();
        ctdt.MaCH = machright;
        ctdt.MaDT = madt;
        obj_taodt.tbl_CHITIETDETHIs.InsertOnSubmit(ctdt);

        var timCHtrongCTDT = (from p in obj_taodt.tbl_CHITIETDETHIs
                             where p.MaDT == madt && p.MaCH==machleft
                             select p).Single();

        obj_taodt.tbl_CHITIETDETHIs.DeleteOnSubmit(timCHtrongCTDT);

        obj_taodt.SubmitChanges();
    }
    
}
